<f:view xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core"
	xmlns:w="http://www.apusic.com/jsf/widget" xmlns:layout="http://www.apusic.com/jsf/layout"
	xmlns:ajax="http://www.apusic.com/jsf/ajax" renderKitId="AJAX"
	xmlns:h="http://java.sun.com/jsf/html" xmlns:om="http://www.apusic.com/jsf/misc">
	<w:head>
		<om:elite>
			<![CDATA[
					define beanClass = param.beanClass;
					define beanId = param.beanId;
					define attachmentType = param.attachmentType;
					
			]]>
		</om:elite>
		<w:script src="/javascript/k4.js" type="text/javascript" language="UTF-8"></w:script>
		<script type="text/javascript">
		//<![CDATA[
			function showUpload(){
				fileUpload.url='#{facesContext.externalContext.requestContextPath}/uploadAttachment?beanClass=#{beanClass}&beanId=#{beanId}&attachmentType=#{attachmentType}';
				fileUpload.show();
			}
			function downloadAttach(){
				var sid = attachmentComponent.getSelectionModel().getSelected().get('id');
				window.location.href='#{facesContext.externalContext.requestContextPath}/downloadAttachment?id='+sid;
			}
			function deleteAttach(){
				if(attachmentComponent.getSelections().length == 0){
					alert("请选择一条记录！");
					return false;
				}
				return confirm('你确定要删除吗？');
			}
			function formatFloat(src, pos){
    			return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
    		}
			function transLength(v, m , r, rIndex){
				if(1024*1024*10>v)
					return formatFloat(v/1024,2)+'KB';
				return formatFloat(v/1024/1024,2)+'MB';
			}
			function reloadAttach(){
				//alert('aaaa');
				var store=attachmentComponent.getStore();
				store.reload(store.lastOptions);
			}
			function closeWindow(){
				fileUpload.hide();
				reloadAttach();
			}
			
			function imageFormat(v){
			  if('doc'==v||'docx'==v){
			    return '<img src="#{facesContext.externalContext.requestContextPath}/images/word.gif"/>';
			  }else if('ppt'==v){
			    return '<img src="#{facesContext.externalContext.requestContextPath}/images/ppt.gif"/>';
			  }else if('zip'==v){
			    return '<img src="#{facesContext.externalContext.requestContextPath}/images/zip.gif"/>';
			  }else if('excel'==v){
			    return '<img src="#{facesContext.externalContext.requestContextPath}/images/excel.gif"/>';
			  }else{
			    return '';
			  }
			  
			 //return '<img src="' + v + '"/>';
			 }
		//]]>
		</script>
	</w:head>
	<w:page title="文件上传">
		<layout:borderLayout fitToBody="true" border="false">
			<layout:panel region="north" height="26" width="100%">
				<w:form groupId="group1">
					<w:toolBar>
						<w:button value="添加附件" id="addFile" alwaysSubmit="false" onclick="javascript:showUpload();" />
						<w:separator></w:separator>
						<w:button value="删除附件" id="delFile" onclick="return deleteAttach();" />
					</w:toolBar>
				</w:form>
			</layout:panel>
			<layout:panel region="center" border="false" autoScroll="true">
				<w:dataGrid border="false" id="attachmentsList" paged="true" showRowNumber="true"
					jsvar="attachmentComponent" selectionModel="#{selectionModel.checkbox}"
					ondblclick="javascript:downloadAttach();" singleSelect="false" autoExpandColumn="2"
					autoHeight="true" toolBarPosition="bottom">
					<w:outputColumn header="ID" id="id" hidden="true"></w:outputColumn>
					<w:outputColumn header="文件名" id="fileName" width="120" wrap="true"></w:outputColumn>
					<w:outputColumn header="类型" id="fileExt" clientFormatter="imageFormat" width="50"></w:outputColumn>
					<w:outputColumn header="长度" id="length" width="100" clientFormatter="transLength"></w:outputColumn>
					<w:outputColumn header="创建时间" id="createTime" width="85" converter="dateFormatter"></w:outputColumn>
					<w:outputColumn header="作者" id="author" width="80"></w:outputColumn>
					<w:pagingToolbar displayInfo="true" displayMsg="显示 {0} - {1}，共 {2} 条" />
				</w:dataGrid>
				<w:fileUploadDialog id="fileUpload" fileSizeMax="2147483648" maximizable="true" show="false"
					minHeight="130" minimizable="true" constrain="true" constrainHeader="true" jsvar="fileUpload"
					onclose="javascript:reloadAttach();" onuploadcomplete="closeWindow()"></w:fileUploadDialog>
			</layout:panel>
		</layout:borderLayout>
	</w:page>
	<script type="text/javascript">
		//<![CDATA[
		Ext.onReady(function (){
			
			fileUpload.width=450;
		});
		//]]>
	</script>
</f:view>
